package cn.zqh.brand.rabbitmq;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.bean.bo.AdminChangeCommodity;
import cn.zqh.bean.bo.AdminChangeUserInfo;
import cn.zqh.brand.client.CommodityClient;
import cn.zqh.brand.client.UserClient;
import cn.zqh.brand.config.BSException;
import com.alibaba.fastjson.JSON;
import org.apache.xmlbeans.impl.xb.xsdschema.RestrictionType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author: zqh
 * @Date: 2023-03-11-11:42
 * @Description: 监听队列$
 */
@Component
public class MyConsumer {

    @Resource
    private UserClient userClient;

    @Resource
    private CommodityClient commodityClient;

    @Autowired
    private RestTemplate restTemplate;

    @RabbitListener(queues = "ms_queue6")
    public void process(Message message){
        byte[] body = message.getBody();
        JSONObject entries = new JSONObject(body);
        String userId = entries.getStr("userId");
        Double money = entries.getDouble("money");
        CommonResult commonResult = restTemplate.postForObject("http://127.0.0.1:9501/user/userReMoney?userId=" + userId + "&money=" + money, "", CommonResult.class);
        if (commonResult.getCode() != 200) {
            throw new BSException(ResultCode.AnErrorOccurredInTheRemoteCall.getCode(),ResultCode.AnErrorOccurredInTheRemoteCall.getMessage());
        }
        System.out.println("用户钱减少成功");
    }




}
